import json
import time
from flask import make_response, request

from .exception import *
from .error_code import *


def response(code, msg, data):
    result_text = json.dumps({
        "code": code,
        "msg": msg,
        "data": data
    }, ensure_ascii=False)
    rst = make_response(result_text)
    # rst.headers['Access-Control-Allow-Origin'] = request.headers.get('origin')
    rst.headers['Access-Control-Allow-Origin'] = 'null'
    rst.headers['Access-Control-Allow-Method'] = '*'
    rst.headers['Access-Control-Allow-Credentials'] = 'true'
    rst.headers['Access-Control-Allow-Header'] = '*'
    return rst


def get_time():
    return time.strftime('%Y-%m-%d %H:%M:%S', time.localtime(time.time()))


def get_type(question_type):
    # 填空-1、选择-2、判断-3、程序填空-4、程序改错-5、编程-6
    if question_type == 1:
        return "填空题"
    elif question_type == 2:
        return "选择题"
    elif question_type == 3:
        return "判断题"
    elif question_type == 4:
        return "程序填空题"
    elif question_type == 5:
        return "程序改错题"
    elif question_type == 6:
        return "编程题"
    else:
        return "WRONG TYPE"


def get_language(language):
    # 0 - C, 1 - C + +, 3 - java, 6 - python
    if language == "C":
        return 0
    elif language == "C++":
        return 1
    elif language == "java":
        return 3
    elif language == "python":
        return 6
    else:
        raise APIException(SEARCH_ERROR)


def get_code_status(code_status):
    # 4-Accept 5-Presentation Error 6-Wrong Answer 7-Time Limit Exceeded 8-Memory Limit Exceeded 9-Output Limit Exceeded
    # 10-Runtime Error 11-Compile Error
    if code_status == 4:
        return "Accept"
    elif code_status == 5:
        return "Presentation Error"
    elif code_status == 6:
        return "Wrong Answer"
    elif code_status == 7:
        return "Time Limit Exceeded"
    elif code_status == 8:
        return "Memory Limit Exceeded"
    elif code_status == 9:
        return "Output Limit Exceeded"
    elif code_status == 10:
        return "Runtime Error"
    elif code_status == 11:
        return "Compile Error"


def get_data():
    data = request.json
    if data is None:
        data = request.form
    return data
    
    
def num2language(num):
    if num == 0:
        return "C"
    elif num == 1:
        return "C++"
    elif num == 3:
        return "Java"
    elif num == 6:
        return "Python"
    else:
        return ""
